<?php // Start by buffering output

ob_start();

// Include important constants and set admin_home (we need to set the variable since file() doesn't interpret constants in pathnames)

require_once("../init.php");
$admin_home = ADMIN_HOME;

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
      <head>
	<title>Historical Out-of-Order data</title>
	<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
	
	<meta http-equiv="content-type" content="application/xhtml+xml; charset=utf-8" />
	<meta name="author" content="Dylan J. Sather" /> 
	<meta name="revised" content= <?php

	/* This script checks the date_modified file to give the date at which a file on the 
	   map was last modified, thus giving us the date at which the map was last revised */

	$date_file = "$admin_home/map_files/archives_date_modified";
	$fh = fopen($date_file, 'r');
	$date = fread($fh, filesize($date_file));
	$date = trim($date);
	fclose($fh);
	print "\"Dylan J. Sather, $date\" />";

	?>
	<meta name="description" content="Grinnell College Campus Lab Map" />
	<meta name="keywords"
		content="Grinnell College, TC Corps, Technology Consultant,
			Computer lab, Map, Dylan J. Sather" />
	<meta http-equiv="Content-Style-Type" content="text/css" />
	<meta http-equiv="Content-Script-Type" content="text/javascript" />
	<link rel="stylesheet" type="text/css" href="../css/labmap.css" />
      </head>	   

<body>

<?php

// Get the date and use it to locate the correct file

/* First, we get our date info (year, month, and day). Some or all of these may be set depending on how far we've traversed...

   Basically, if we get here from oo_stats.php, we'll have a year (in oo_stats.php, we read the contents of our oo_stats directory
   and see what we've got. The names of these directories should be years (e.g. '2008', '2009')). Once we get here for the first time, 
   then, we'll at least have the year the user chooses. Therefore, we can list the contents of that directory (which will be months),
   and move from there. Once we choose a month, we'll have the list of days, which will be data files, from which we can finally read
   the out-of-order stats */

$year = $_GET['year'];
$month = $_GET['month'];
$day = $_GET['day'];

// First, let's check if we've got a year (if we came from oo_stats.php, we should be able to GET this)
if (isset($year)) {

	// Then check for month
	if (isset($month)) {

		// And finally, a day. If we have a day, we should be able to finally print the info we need
		if (isset($day)) {

			/* We want to print $day in a nicer format than just the filename, so let's get the day by exploding
		           the filename and getting the last element of the array */
			$day_array = explode ('_', $day);

			print "<h2 class=\"centeralign\">Historical Out-of-Order statistics for $month $day_array[2], $year</h2>
	                <p class=\"centeralign\">";

			$oo_file = "$admin_home/printers/oo_status/old_stats/$year/$month/$day";
			$fh = fopen($oo_file, 'r');
			$data = fread($fh, filesize($oo_file));
			fclose($fh);

			print "<pre>$data</pre>";
		}
		else {
			// If we've got a month but not a day, list the contents of the month directory and print as links

			$oo_stats_dir = "$admin_home/printers/oo_status/old_stats/$year/$month";
        		$files = scandir ($oo_stats_dir);

			print "<h2 class=\"centeralign\">Historical Out-of-Order statistics for $month $year, filed by day</h2>
	                <p class=\"centeralign\">";

		        foreach ($files as $_ => $oo_stat_day) {

        		        // If we've got a virtual directory, don't list it
                		if ($oo_stat_day == '.' || $oo_stat_day == '..') {
					// Do nothing
				}
				else {
					// Make links pretty
					$day_array = explode ('_', $oo_stat_day);

        		                print "<a href=\"archives.php?year=$year&month=$month&day=$oo_stat_day\">$month $day_array[2], $year</a><br />\n";
	                	}
        		}
		}
	}
	else {
		// If we've only got a year, list the contents of the year directory and print as links

		$oo_stats_dir = "$admin_home/printers/oo_status/old_stats/$year";
		$files = scandir ($oo_stats_dir);

		print "<h2 class=\"centeralign\">Historical Out-of-Order statistics for $year, filed by month</h2>
                <p class=\"centeralign\">";

		foreach ($files as $_ => $oo_stat_month) {

			// If we've got a virtual directory, don't list it
			if ($oo_stat_month == '.' || $oo_stat_month == '..') {
				// Do nothing
			}
			else {
				/* There's an issue here with the sorting of directories into the proper order. By default, scandir lists files
				   in alphabetical order, which is fine for our numerical files (i.e. years and days), since it prints them in
				   numberical order. But for our months, this presents a problem, since February comes before January, et al. .
				   Therefore, we have to find the modification time with filemtime() and create an array where file => modification time.
				   Then we can print the directories in the right order */

				$time_modified = filemtime("$admin_home/printers/oo_status/old_stats/$year/$oo_stat_month");
				$file_modtime["$oo_stat_month"] = $time_modified;
			}
		}

		// Now, let's sort the array we've created, keeping associations
		asort ($file_modtime);

		// And print the sorted array
		foreach ($file_modtime as $month => $modtime) {
			print "<a href=\"archives.php?year=$year&month=$month\">$month</a><br />\n";
		}
	}
}

?>

</body>

</html>

<?php // End by flushing output

ob_end_flush();

?>
